var ctx;
var name = "";
var houseType;
var houseState;
var regionId;
var pSize;
var selectRu = [];
var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引

function setCtx(ctx) {
    this.ctx = ctx;
}

/**
 * 程序入口
 */
$(function() {
});

function gotoPage(page) {
    name = ifNull($("#name").val());
    houseType = ifNull($("#houseType").val());
    houseState = ifNull($("#houseState").val());
    regionId = ifNull($("#regionId").val());
    pSize = ifNull($("#pSize").val());
    // 解决ie9以下placeholder与value重复的问题
    if (name == $("#name").attr("placeholder")) {
        name = "";
    }

    loadContent("/cellHouse/cellHouseApplication?pNo=" + page + "&name=" + name
            + "&houseType=" + houseType + "&houseState=" + houseState
            + "&regionId=" + regionId + "&pSize="+ pSize);
}

function ifNull(mess) {
    if (mess == null || mess == undefined) {
        mess = "";
    }

    return mess;
}

//条件搜索用户列表
function getCellHouseList() {
    name = ifNull($("#name").val());
    var applyState = $("#applyState").val();
    houseType = ifNull($("#houseType").val());
    houseState = ifNull($("#houseState").val());
    regionId = ifNull($("#regionId").val());
    pSize = ifNull($("#pSize").val());
    // 解决ie9以下placeholder与value重复的问题
    if (name == $("#name").attr("placeholder")) {
        name = "";
    }
    loadContent("/cellHouse/cellHouseApplication?pNo=1&name=" + name + "&houseType="
            + houseType + "&houseState=" + houseState + "&regionId=" + regionId + "&pSize="+ pSize+ "&state="+ applyState);

}

function gotoCellHousePage(page) {
    if (page == null) {
        page = $(".inputclass").val();
    }
    name = ifNull($("#name").val());
    houseType = ifNull($("#houseType").val());
    houseState = ifNull($("#houseState").val());
    regionId = ifNull($("#regionId").val());
    pSize = ifNull($("#pSize").val());
    // 解决ie9以下placeholder与value重复的问题
    if (name == $("#name").attr("placeholder")) {
        name = "";
    }

    loadContent("/cellHouse/cellHouseApplication?pNo=" + page + "&name=" + name
            + "&houseType=" + houseType + "&houseState=" + houseState
            + "&regionId=" + regionId + "&pSize="+ pSize);
}

function detailCellHouseInfo(id) {
    loadContent("/cellHouse/cellHouseDetail?id=" + id + "");
}

// 新增或编辑用户(1:新增 2:编辑)
function saveOrUpdate(id) {
    if (id != null) {
        loadContent("/cellHouse/editCellHouse?id=" + id + "");
    } else {
        loadContent("/cellHouse/editCellHouse?id=0");
    }

}

// 全选和反选
function selectAll(target) {
    if (target.checked) {
        $("#tableTbody :checkbox").attr("checked", true);
        $(target).parents("thead").siblings("tbody").children().children()
                .each(function() {
                    $(this).css("background", "#D3F0F1");
                });
    } else {
        $("#tableTbody :checkbox").attr("checked", false);
        $(target).parents("thead").siblings("tbody").children().children()
                .each(function() {
                    $(this).css("background", "");
                });
    }

}

// 鼠标滑入一行改变背景颜色
function mouseoverOne(target) {
    $(target).children().each(function() {
        $(this).css("background", "#D3F0F1");
    });
}

// 鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target) {
    $(target).children().each(function() {
        $(this).css("background", "");
    });
}

// 选择或取消选中当前记录
function selectOne(id, target) {
    if (target.checked) {
        $("#house" + id).attr("checked", true);
        $(target).parents("tr").children().each(function() {
            $(this).css("background", "#D3F0F1");
        });
    } else {
        $("#house" + id).attr("checked", false);
        $(target).parents("tr").children().each(function() {
            $(this).css("background", "");
        });
    }
}

function deleteByIds() {
    if ($("img[name='houseChecked'][src*=honggou]").length == 0) {
        layer.msg("请选择要删除的记录!", {
            icon : 3,
            time : 2000
        // 2秒关闭（如果不配置，默认是3秒）
        });
    } else {
        var delIds = [];
        $("#tableTbody img[name='houseChecked'][src*=honggou]").each(
                function() {
                    delIds.push($(this).attr("class"));
                });
        var vals = delIds.join(',');// 转换为逗号隔开的字符串
        layer.confirm('确认删除?', {
            icon : 3,
            title : '提示'
        }, function(index) {
            // do something
            $.post(ctx + "/cellHouse/deleteCellHouse", {
                houseIDs : vals
            }, function(data) {
            	data = JSON.parse(data);
                if (data.success) {
                    layer.msg("删除成功", {
                        icon : 1,
                        time : 2000
                    // 2秒关闭（如果不配置，默认是3秒）
                    }, function() {
                        loadContent("/cellHouse/cellHouseList");
                    });
                } else {
                    layer.msg(data.msg, {
                        icon : 2,
                        time : 2000
                    // 2秒关闭（如果不配置，默认是3秒）
                    });
                }
            });
            layer.close(index);
        });

    }
}

function updateSort(houseId) {
    if (houseId != null) {
        $.get(ctx + "/cellHouse/updateSort", {
            id : houseId
        }, function(data) {
        	data = JSON.parse(data);
            if (data.success) {
                layer.msg("操作成功", {
                    icon : 1,
                    time : 2000
                // 2秒关闭（如果不配置，默认是3秒）
                }, function() {
                    loadContent("/cellHouse/cellHouseList");
                });
            } else {
                layer.msg(data.msg, {
                    icon : 2,
                    time : 2000
                // 2秒关闭（如果不配置，默认是3秒）
                });
            }
        });
    }
}

// 实现字段较长时的隐藏于显示
var index;
function mouseOverOName(item, oName) {
    index = layer.tips(oName, '#oName' + item, {
        tips : [ 3, '#37CED4' ]
    });
}

function mouseOutOName(item, oName) {
    layer.close(index);
}

/*房屋申请--审核--状态更新*/
function approve(id, state) {
    var message;
    var ownerHouseId = $("#ownerHouseId").val();
    var ownerId = $("#ownerId").val();
    var detailAddress = $("#detailAddress").val();
    var regionId = $("#regionId").val();
    var buildingId = $("#buildingId").val();
    var unitId = $("#unitId").val();
    var recepientName = $("#recepientName").val();
    var recepientPhone = $("#recepientPhone").val();
    switch (parseInt(state)) {
    case 2:
        message = "是否予以通过审核？";
        break;
    case 4:
        message = "是否不予以通过审核？";
        break;
    default:
        break;
    }
    layer.confirm(message, {
        icon : 3,
        title : '提示'
    }, function(index) {
        $.ajax({
            type : "POST",
            url : ctx + "/cellHouse/cellHouseUpdating",
            cache : false,
            dataType : 'json',
            data : {
                ownerId: ownerId,
                ownerHouseId: ownerHouseId,
                id : id,
                state : state,
                regionId :regionId,
                buildingId :buildingId,
                unitId :unitId,
                recepientName :recepientName,
                recepientPhone :recepientPhone,
                detailAddress : detailAddress
            },
            success : function(result) {
                if (result.success) {

                    layer.msg("操作成功", {
                        icon : 1,
                        time : 1000
                    // 2秒关闭（如果不配置，默认是3秒）
                    }, function() {
                        loadContent("/cellHouse/cellHouseApplication");
                        if(result.obj != 0) {
                            $('#unReading').show();
                            $('#unReading').html(result.obj);
                        } else {
                            $('#unReading').hide();
                        }
                    });
                } else {
                    layer.msg("操作失败", {
                        icon : 2
                    });
                }
            }
        });
        layer.close(index);
    });
}



//批量导入
function initCellHouse () {
    url = ctx + "/cellHouse/initList";
    msg = "批量导入";
    importCellHouseList(msg, url);
}

var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//弹出导入小区房屋信息框
function importCellHouseList(msg,url){
      //官网欢迎页
       index = layer.open({
             type: 2,
             //skin: 'layui-layer-lan',
             title: msg,
             fix: false,
             shadeClose: true,
             scrollbar: false,
             maxmin: false,
             edit: true,
             closeBtn: 1,
             area: ['500px', '220px'],
             content: url,
             btn: ['设置', '取消']
               ,yes: function(index, layero){ //或者使用btn1
                   //按钮【按钮一】的回调
                var iframeWin = window[layero.find('iframe')[0]['name']]; 
                iframeWin.importCellHouse();

               },cancel: function(index){ //或者使用btn2
                   //按钮【按钮二】的回调
        }
    });
}


//导出小区房屋信息
function exportExcel() {
    total = ifNull($("#total").val());
    name = ifNull($("#name").val());
    houseType = ifNull($("#houseType").val());
    houseState = ifNull($("#houseState").val());
    regionId = ifNull($("#regionId").val());
    // 解决ie9以下placeholder与value重复的问题
    if (name == $("#name").attr("placeholder")) {
        name = "";
    }
    if(total >0) {
        location.href=ctx +"/cellHouse/exportCellHouseExcel?name=" + name + "&houseType="
            + houseType + "&houseState=" + houseState + "&regionId=" + regionId;
    } else {
        layer.msg("未获取到数据，无法导出...",{
            icon: 2,
            time: 2000 //2秒关闭（如果不配置，默认是3秒）
        });
    }
}


//实现字段较长时的隐藏于显示
var index;
function mouseOverOName(item, oName) {
	index = layer.tips(oName, '#oName' + item, {
		tips : [ 3, '#37CED4' ]
	});
}

function mouseOutOName(item, oName) {
	layer.close(index);
}

function showDetail(id) {
    loadContent("/cellHouse/showDetail?id="+id);
}

// 新增备注
function addMemo(id) {
    if (null == id || "" == id || undefined == id) {
        layer.msg("用户没有关联的房屋，不能添加审核备注！！！", {
            icon : 2,
            time : 2000
        // 2秒关闭（如果不配置，默认是3秒）
        });
    } else {
        msg = "添加备注";
        url = ctx + "/cellHouse/addMemo?id=" + id;
        saveOrUpdateMemo(msg, url, id);
    }
}
    


// 保存备注信息
index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
function saveOrUpdateMemo(msg, url, id) {
    // 官网欢迎页
    index = layer.open({
        type : 2,
        // skin: 'layui-layer-lan',
        title : msg,
        fix : false,
        shadeClose : true,
        scrollbar : false,
        maxmin : false,
        edit : true,
        closeBtn : 1,
        area : [ '500px', '230px' ],
        content : url,
        btn : [ '确定', '取消' ],
        yes : function(index, layero) { // 或者使用btn1
            // 按钮【按钮一】的回调
            var iframeWin = window[layero.find('iframe')[0]['name']];
            iframeWin.saveMemo(id);
        },
        cancel : function(index) { // 或者使用btn2
            // 按钮【按钮二】的回调
        }
    });
}
